﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Papyrus.Web.Models;
using Papyrus.Web.Models.Entities;

namespace Papyrus.Web.Controllers
{
    public class CustomerController : Controller
    {
        private readonly ICustomerRepository repository;

        public CustomerController()
            : this(new CustomerRepository())
        {
        }

        public CustomerController(ICustomerRepository repository)
        {
            this.repository = repository;
        }

        //
        // GET: /Customer/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Customer/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Customer/Create

        public ActionResult Create()
        {
            return this.View("Create", new CustomerViewModel());
        }

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        [ValidateInput(true)]
        public ActionResult Save(CustomerViewModel customerViewModel)
        {
            this.repository.Save(
                new Customer
                {
                    id = customerViewModel.Id,
                    Address = customerViewModel.Address,
                    DocumentId = customerViewModel.DocumentId,
                    Firstname = customerViewModel.Firstname,
                    Lastname = customerViewModel.Lastname,
                    PhoneNumber1 = customerViewModel.PhoneNumber1,
                    PhoneNumber2 = customerViewModel.PhoneNumber2,
                });

            return this.View();
        }

        
        //
        // GET: /Customer/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Customer/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
